package com.start.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class WindowCondition implements Condition {
	/**
	 * ConditionContext:判断条件，能使用的上下文（环境）
	 * AnnotatedTypeMetadata：当前标注了@Conditional注解的注释信息
	 */
	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// 1.获取到IOC使用的beanFactory工厂
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		// 2.获取类加载器
		ClassLoader classLoader = context.getClassLoader();
		// 3.获取当前环境信息
		Environment environment = context.getEnvironment();
		// 4.获取到Bean定义的注册类：所有Bean都在BeanDefinitionRegistry类里注册，能在这里查看已定义的Bean，也能用它注册一个新Bean
		BeanDefinitionRegistry registry = context.getRegistry();
		
		String property = environment.getProperty("os.name");
		System.out.println("********>"+property);
		if (property.contains("Windows")) {
			System.out.println("WindowCondition.matches()");
			return true;
		}
		return false;
	}

}
